package com._520it.crm.web.controller;

import com._520it.crm.domain.RenewalInsurance;
import com._520it.crm.page.PageResult;
import com._520it.crm.query.RenewalInsuranceQueryObject;
import com._520it.crm.service.IRenewalInsuranceService;
import com._520it.crm.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

/**
 * Created by 呆 on 2017/10/15.
 */
@Controller
@RequestMapping("/renewal")
public class RenewalInsuranceController {
    @Autowired
    private IRenewalInsuranceService renewalInsuranceService;

    @RequestMapping("")
    public String index(){
        return "renewalInsurance";
    }

    @RequestMapping("/list")
    @ResponseBody
    public PageResult list(RenewalInsuranceQueryObject qo){

        return renewalInsuranceService.query(qo);

    }

    @RequestMapping("/save")
    @ResponseBody
    public AjaxResult save(RenewalInsurance renewalInsurance){
        AjaxResult result = null;
        try{

            renewalInsuranceService.insert(renewalInsurance);
            result = new AjaxResult(true,"保存成功");
        }catch (Exception e){
            result = new AjaxResult("保存失败");
        }

        return result;
    }

    /**
     * 提交申请续保时保存 续保信息
     * @param renewalInsurance 续保信息对象
     * @param orderId 关联的保单id
     * @return
     */
    @RequestMapping("/saveRenewal")
    @ResponseBody
    public AjaxResult saveRenewal(RenewalInsurance renewalInsurance ,Long orderId){
        AjaxResult result = null;
        try{
            renewalInsurance.setApplyRenewTime(new Date());
            renewalInsurance.setOrderBillId(orderId);
            renewalInsuranceService.insert(renewalInsurance);
            result = new AjaxResult("申请成功");
        }catch (Exception e){
            e.printStackTrace();
            result = new AjaxResult(false,"申请失败");
        }

        return result;
    }
}
